package com.ruoyi.edu.controller;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.edu.domain.EduSubject;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.edu.domain.EduTeacher;
import com.ruoyi.edu.service.IEduTeacherService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 讲师Controller
 * 
 * @author niruichen
 * @date 2020-12-22
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/edu/teacher" )
public class EduTeacherController extends BaseController {

    private final IEduTeacherService iEduTeacherService;

    /**
     * 查询讲师列表
     */
    @PreAuthorize("@ss.hasPermi('edu:teacher:list')")
    @GetMapping("/list")
    public TableDataInfo list(EduTeacher eduTeacher) {
        startPage();
        List<EduTeacher> list = iEduTeacherService.queryList(eduTeacher);
        return getDataTable(list);
    }

    /**
     * 导出讲师列表
     */
    @PreAuthorize("@ss.hasPermi('edu:teacher:export')" )
    @Log(title = "讲师" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(EduTeacher eduTeacher) {
        List<EduTeacher> list = iEduTeacherService.queryList(eduTeacher);
        ExcelUtil<EduTeacher> util = new ExcelUtil<EduTeacher>(EduTeacher.class);
        return util.exportExcel(list, "teacher" );
    }

    /**
     * 获取讲师详细信息
     */
    @PreAuthorize("@ss.hasPermi('edu:teacher:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(iEduTeacherService.getById(id));
    }

    /**
     * 新增讲师
     */
    @PreAuthorize("@ss.hasPermi('edu:teacher:add')" )
    @Log(title = "讲师" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduTeacher eduTeacher) {
        return toAjax(iEduTeacherService.save(eduTeacher) ? 1 : 0);
    }

    /**
     * 修改讲师
     */
    @PreAuthorize("@ss.hasPermi('edu:teacher:edit')" )
    @Log(title = "讲师" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduTeacher eduTeacher) {
        return toAjax(iEduTeacherService.updateById(eduTeacher) ? 1 : 0);
    }

    /**
     * 删除讲师
     */
    @PreAuthorize("@ss.hasPermi('edu:teacher:remove')" )
    @Log(title = "讲师" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(iEduTeacherService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 查询讲师下拉框
     */
    @GetMapping("/data")
    public AjaxResult data() {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name");
        List<EduTeacher> list = iEduTeacherService.list(wrapper);
        return AjaxResult.success(list);
    }
}
